<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;
use App\Common\ConstCode;
class Bubble extends Authenticatable
{

    use HasApiTokens,Notifiable;


    protected $table = 'bubble';
    protected $fillable = ['type','category','pic_uri as cover_uri','created_at','id','uid'];

    public    $timestamps = false;
    protected $primaryKey = 'id';


    public function lists($data,$uid){
        $categorys=config('cartoontag.BUBBLE');
        $curCount = count($categorys);
        //$keys=array_keys($categorys);
        $where=[
            'is_shelf'=>ConstCode::YES,
            'status'=>ConstCode::YES
        ];
        $result=DB::table('bubble')->where($where)->select($this->fillable)
            ->groupBy('category')
            ->orderBy(ConstCode::COLUMN, ConstCode::ASC_DIRECTION)
            ->get()
            ->toArray();
        foreach ($result as &$val){
            $val->cover_uri=uri_fill($val->cover_uri);
            $val->title=$categorys[$val->category];
            $val->is_my=0;
            $val->classify=config('cartoontag.ALBUM_TYPE.气泡库');
        }
        $myCount=$this->count(['uid'=>$uid]);
        $data = [
            'list'   =>  $result,
            'myCount'  =>  $myCount,
            'curCount' =>  $curCount,
            'myCollect'=>  0
        ];
        return $data;
    }

    public function count($where){
        $count=DB::table('bubble')->where($where)->count();
        return $count;
    }

    public function my_lists($data,$uid){
        $where=['uid'=>$uid];

        $result=DB::table('bubble')->where($where)
            ->select('type','pic_uri as cover_uri','status','created_at','id','category')
            ->forPage($data['page'], $data['pageSize'])
            ->orderBy(ConstCode::COLUMN, ConstCode::DIRECTION)
            ->get()
            ->toArray();
        if($result){
            foreach ($result as &$val){
                $categorys=config('cartoontag.BUBBLE');
                $val->cover_uri=uri_fill($val->cover_uri);
                $val->title=$categorys[$val->category];
            }
        }
        $curCount = $this->count($where);
        $data = [
            'list'   =>  $result,
            'total' =>  $curCount,
        ];
        return $data;
    }
}
